package com.lxy.user.controller;

import com.lxy.api.client.EquipmentClient;
import com.lxy.api.domain.vo.CompletedRepairVO;
import com.lxy.api.domain.vo.RepairVO;
import com.lxy.common.domain.common.R;
import com.lxy.common.domain.vo.MaintenanceSchedulesVO;
import com.lxy.common.utils.UserContext;
import com.lxy.user.domain.dto.CompleteRepairDTO;
import com.lxy.user.domain.dto.LoginFormDTO;
import com.lxy.user.domain.dto.RegistrationDTO;
import com.lxy.user.domain.dto.RepairDTO;
import com.lxy.user.domain.po.DailyMaintenance;
import com.lxy.user.domain.po.User;
import com.lxy.user.domain.vo.CodeVO;
import com.lxy.user.domain.vo.ForgetPasswordVO;
import com.lxy.user.domain.vo.UserLoginVO;
import com.lxy.user.enums.Permission;
import com.lxy.user.service.UserFaceService;
import com.lxy.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;


@CrossOrigin

@Api(tags = "用户相关接口")
@RestController
@RequiredArgsConstructor
@RequestMapping("/users")
public class UserController {

    private final UserService userService;
    private final EquipmentClient equipmentClient;
    private final UserFaceService userFaceService;



    @ApiOperation("用户登录接口")
    @PostMapping("/login")
    public R<UserLoginVO> login(@RequestBody LoginFormDTO loginFormDTO){
       return userService.login(loginFormDTO);
    }

    @ApiOperation("用户注册接口")
    @PostMapping("/registration")
    //TODO LoginFormDTO改为VO
    public R<LoginFormDTO> registration(@RequestBody RegistrationDTO registrationDTO) throws Exception {
        return userService.registration(registrationDTO);
    }

    @ApiOperation("发送验证码接口(注册用)")
    @PostMapping("/sendCode")
    public R sendCode(@RequestBody CodeVO codeVO) throws Exception {
        return userService.sendMail(codeVO.getMailTitle(),null, codeVO.getEmail());
    }

    @ApiOperation("故障报修")
    @PostMapping("/repairRequests")
    public R repairRequests(@RequestBody RepairDTO repairDTO){
        return userService.malfunctionRepair(repairDTO);
    }

    @ApiOperation("完成维修订单")
    @PostMapping("/completeRepair")
    public R completeRepair(@RequestBody CompleteRepairDTO completeRepairDTO){

        //判断是否有权限
        Long userId = UserContext.getUser();
        User user = userService.getById(userId);
        if(user.getPermission().equals(Permission.REPAIR_USER)||user.getPermission().equals(Permission.DEVICE_ADMINISTRATOR)){
            return userService.completeRepair(completeRepairDTO);
        }else{
            return R.error("无权限进行操作");
        }
    }

    @ApiOperation("查看自己的维修订单（维修人员）")
    @GetMapping("/getMyRepair")
    public R<List<RepairVO>> getMyRepair(){
        //使用OpenFeign通讯
        return equipmentClient.getMyRepair();
    }

    @ApiOperation("查看自己的历史维修订单（维修人员）")
    @GetMapping("/getMyCompletedRepair")
    public R<List<CompletedRepairVO>> getMyCompletedRepair(){
        //使用OpenFeign通讯
        return equipmentClient.getMyCompletedRepair();
    }

    @ApiOperation("提交每日维护报告")
    @PostMapping("/submitDailyLog")
    public R submitDailyLog(@ApiParam("维护日志") @RequestParam("log") String log){
        return userService.submitDailyLog(log);
    }

    @ApiOperation("查看自己的维护报告（分页查询）")
    @GetMapping("/getMyDailyMaintenance")
    public  R<List<DailyMaintenance>> getMyDailyMaintenance(){
        return userService.getMyDailyMaintenance();
    }

    @ApiOperation("找回密码")
    @GetMapping("/forgetPassword")
    public R forgetPassword(ForgetPasswordVO forgetPasswordVO){
        return userService.forgetPassword(forgetPasswordVO);
    }

    @ApiOperation("更改维修单状态为维修中(点击开始维修)")
    @GetMapping("/changeRepairStatus")
    public R changeRepairStatus(@RequestParam("repairId") Long repairId){
        return userService.changeRepairStatus(repairId);
    }

    @ApiOperation("查看自己的维护计划（维修人员）")
    @GetMapping("/getMyMaintenanceSchedules")
    public R<List<MaintenanceSchedulesVO>> getMyMaintenanceSchedules(){
        return userService.getMyMaintenanceSchedules();
    }

    @ApiOperation("文件上传")
    @PostMapping("/upload")
    //MultipartFile 定义的实体类名称需要和前端传来的数据名称一致
    public R<String> upload(MultipartFile file){
        return userService.upload(file);
    }

    @ApiOperation("人脸识别")
    @PostMapping("/face")
    public R face(MultipartFile file) throws Exception {
        return userFaceService.face(file);
    }





}
